package com.housediyadmin.view
{
	import com.adobe.cairngorm.process.ProcessEvent;
	import com.housediyadmin.events.bubbleEvents.CheckRoomIDBubbleEvt;
	import com.housediyadmin.events.bubbleEvents.DeleteRoomBubbleEvt;
	import com.housediyadmin.events.bubbleEvents.EditRoomBubbleEvt;
	import com.housediyadmin.events.roomEvents.AddRoomEvent;
	import com.housediyadmin.events.roomEvents.DeleteRoomEvent;
	import com.housediyadmin.events.roomEvents.GetRoomEvent;
	import com.housediyadmin.model.RoomModel;
	import com.housediyadmin.view.components.RoomPopUp;
	import com.housediyadmin.vo.Room;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	
	import flashx.textLayout.events.ModelChange;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.core.IFlexDisplayObject;
	import mx.events.CloseEvent;
	import mx.events.ListEvent;
	import mx.events.Request;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.events.IndexChangeEvent;
	
	/**
	 * 
	 * @author yanghanbing
	 * 2010/11/24
	 */	
	public class RoomAdminMediator extends Mediator
	{
		[Inject]
		public var view:RoomAdminView;

		[Inject]
		public var roomModel:RoomModel ;

		override public function onRegister():void
		{
			BindingUtils.bindProperty(view.roomList, "dataProvider", roomModel, "roomArray");		
			view.roomBtn.addBtn.addEventListener(MouseEvent.CLICK,popUpAddRoomView);
			view.roomBtn.deleteBtn.addEventListener(MouseEvent.CLICK, deleteAlert);
			//侦听Renterer里面的DeleteRoomBubbleEvt事件
			view.addEventListener(DeleteRoomBubbleEvt.DELETE_A_ROOM, deleteAlertHandler);
			//侦听Renterer里面的DeleteRoomBubbleEvt事件
			view.addEventListener(EditRoomBubbleEvt.EDIT_A_ROOM, popUpEditRoomView);
			view.addEventListener(CheckRoomIDBubbleEvt.ADD_TO_ROOMIDS, addIdToRoomIdsHandler);
			dispatch(new GetRoomEvent(GetRoomEvent.GET_ROOM_MODEL, null));			
		}
        
		private function popUpEditRoomView(event:EditRoomBubbleEvt):void
		{
			roomModel.currentRoom = event.room;
			var popUpEditView:RoomPopUp = new RoomPopUp();
			popUpEditView.currentState = "edit";
			PopUpManager.addPopUp(popUpEditView, contextView);
			PopUpManager.centerPopUp(popUpEditView);
			mediatorMap.createMediator(popUpEditView);
			popUpEditView.addEventListener(CloseEvent.CLOSE, close);
		}
		
		private function addIdToRoomIdsHandler(event:CheckRoomIDBubbleEvt):void
		{
			var roomIdsArr:ArrayCollection = new ArrayCollection(roomModel.roomIds);
			if(roomIdsArr.contains(event.roomId))
			{
				var currentIndex:int = roomIdsArr.getItemIndex(event.roomId);
				roomIdsArr.removeItemAt(currentIndex);
				roomModel.roomIds = roomIdsArr.source;
			}
			else
			{
				roomModel.roomIds.push(event.roomId);
			}			
		}
		
		public function deleteAlertHandler(event:DeleteRoomBubbleEvt):void
		{
		   	Alert.yesLabel = "是";
			Alert.noLabel = "否";
			Alert.show("删除这个房型信息吗？", "", Alert.YES | Alert.NO, view, deleteOneRoomHandler);
			roomModel.roomIds = new Array();
			roomModel.roomIds.push(event.roomId);
		}
		
		public function deleteOneRoomHandler(event:CloseEvent):void
		{		
			if(event.detail == Alert.YES)
			{
				dispatch(new DeleteRoomEvent(DeleteRoomEvent.DELETE_ROOM, roomModel.roomIds));
			}
			roomModel.roomIds = new Array();
		}
		
		private function popUpAddRoomView(event:MouseEvent):void
		{		
			var popUpAddView:RoomPopUp = new RoomPopUp();			
			PopUpManager.addPopUp(popUpAddView, contextView);
			mediatorMap.createMediator(popUpAddView);
			PopUpManager.centerPopUp(popUpAddView);
			popUpAddView.addEventListener(CloseEvent.CLOSE, close);
			popUpAddView.currentState = "add";			
			roomModel.currentRoom = new Room();			
		}
		
		private function deleteAlert(event:MouseEvent):void
		{
			if(roomModel.roomIds.length == 0 )
			{
				Alert.show("请选择需要删除的房型");
			}
			else
			{
				Alert.yesLabel = "是";
				Alert.noLabel = "否";
				Alert.show("删除这些房型信息吗？", "", Alert.YES | Alert.NO, view, deleteRoomsHandler);	
			}			
		}
		
		private function deleteRoomsHandler(event:CloseEvent):void
		{			
			if(event.detail == Alert.YES)
			{
				dispatch(new DeleteRoomEvent(DeleteRoomEvent.DELETE_ROOM, roomModel.roomIds));
			}
			roomModel.roomIds = new Array();
		}
		
		public function close(event:CloseEvent):void
		{
			event.target.removeEventListener(CloseEvent.CLOSE, close);
			PopUpManager.removePopUp(event.target as IFlexDisplayObject);
			mediatorMap.removeMediatorByView(event.target as IFlexDisplayObject);
			roomModel.currentRoom = new Room();
		}
	}
}